<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学科" prop="subjectId">
        <el-select v-model="queryParams.subjectId" placeholder="学科" clearable>
          <el-option v-for="dict in subjectList" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="教师" prop="teacherId">
        <el-select v-model="queryParams.teacherId" placeholder="教师" clearable>
          <el-option v-for="dict in teacherList" :key="dict.teacherId" :label="dict.teacher.name"
            :value="dict.teacherId" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:course:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:course:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:course:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:course:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="courseList" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item>
              <el-button size="mini" type="text" @click="handleCourseContent(props.row)">课程内容</el-button>
            </el-form-item>
          </el-form>

          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item>
              <el-button size="mini" type="text" @click="handleCourseVideo(props.row)">教学视频</el-button>
            </el-form-item>
          </el-form>

          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item>
              <el-button size="mini" type="text" @click="handleCourseExercises(props.row)">课后习题</el-button>
            </el-form-item>
          </el-form>

          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item>
              <el-button size="mini" type="text" @click="handleCourseHomework(props.row)">课程作业</el-button>
            </el-form-item>
          </el-form>

          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item>
              <el-button size="mini" type="text" @click="handleCourseRecord(props.row)">考勤</el-button>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>


      <el-table-column label="课程编号" align="center" prop="id" />
      <el-table-column label="教师编号" align="center" prop="teacherId" />
      <el-table-column label="课程名称" align="center" prop="name" />
      <el-table-column label="课程介绍" align="center" prop="introduction" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:course:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:course:remove']">删除</el-button>
          <el-button size="mini" type="text" @click="handleAttendanceRecord(scope.row)"
             v-if="!scope.row.attendance && roleCode == 'common'">签到</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改课程管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入课程名称" />
        </el-form-item>
        <el-form-item label="课程介绍" prop="introduction">
          <el-input v-model="form.introduction" placeholder="请输入课程介绍" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改课程内容对话框 -->
    <el-dialog :title="title" :visible.sync="openCourseContent" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入课程名称" />
        </el-form-item>
        <el-form-item label="课程内容">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="课程内容图片" prop="contentImgs">
          <image-upload v-model="form.contentImgs" />

        </el-form-item>
        <el-form-item label="课程文件" prop="contentFile">
          <file-upload v-model="form.contentFile" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="roleCode == 'teacher'" @click="submitContentForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改教学视频管理对话框 -->
    <el-dialog :title="title" :visible.sync="openCourseVideo" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="视频名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入视频名称" />
        </el-form-item>
        <el-form-item label="视频文件" prop="videoFile">
          <file-upload-video v-model="form.videoFile" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="roleCode == 'teacher'" @click="submitVideoForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



    <!-- 添加或修改课后习题管理对话框 -->
    <el-dialog :title="title" :visible.sync="openCourseExercises" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课后习题内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="课后习题图片" prop="contentImgs">
          <image-upload v-model="form.contentImgs" />
        </el-form-item>
        <el-form-item label="课后习题文件" prop="contentFile">
          <file-upload v-model="form.contentFile" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="roleCode == 'teacher'" @click="submitopenCourseExercisesForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改作业管理对话框 -->
    <el-dialog :title="title" :visible.sync="openCourseHomework" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="作业内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="作业内容图片" prop="contentImgs">
          <image-upload v-model="form.contentImgs"/>
        </el-form-item>
        <el-form-item label="作业内容文件" prop="contentFile">
          <file-upload v-model="form.contentFile"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="roleCode == 'teacher'" @click="submitHomeworkForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改考勤管理对话框 -->
    <el-dialog :title="title" :visible.sync="openCourseRecord" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程作业答题图片" prop="homeworkAnswer">
          <image-upload v-model="form.homeworkAnswer"/>
        </el-form-item>
        <el-form-item label="课程作业分数" prop="homeworkScore">
          <el-input v-model="form.homeworkScore" :disabled="roleCode == 'common'" placeholder="请输入课程作业分数" />
        </el-form-item>
        <el-form-item label="课后习题答题图片" prop="homeworkExercisesAnswer">
          <image-upload v-model="form.homeworkExercisesAnswer"/>
        </el-form-item>
        <el-form-item label="客户习题答题分数"  prop="homeworkExercisesScore">
          <el-input v-model="form.homeworkExercisesScore" :disabled="roleCode == 'common'" placeholder="请输入客户习题答题分数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitRecordForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



  </div>
</template>

<script>
import { listContent, getContent, delContent, addContent, updateContent } from "@/api/system/content";
import { listVideo, getVideo, delVideo, addVideo, updateVideo } from "@/api/system/video";
import { listExercises, getExercises, delExercises, addExercises, updateExercises } from "@/api/system/exercises";
import { listHomework, getHomework, delHomework, addHomework, updateHomework } from "@/api/system/homework";
import { listCourse, getCourse, delCourse, addCourse, updateCourse } from "@/api/system/course";
import { listTeacher, getTeacher, delTeacher, addTeacher, updateTeacher, changeTeacherApplicationStatus, hireTeacher } from "@/api/system/teacher";
import { getAllSubjectList } from "@/api/system/subject";
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/system/record";

export default {
  name: "Course",
  data() {
    return {
      openCourseExercises: false,
      openCourseHomework: false,
      openCourseRecord: false,
      openCourseVideo: false,
      openCourseContent: false,
      roleCode: '',
      subjectList: [],
      teacherList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程管理表格数据
      courseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherId: null,
        name: null,
        parentId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.roleCode = localStorage.getItem("roles")
    console.log(this.roleCode)
    getAllSubjectList().then(res => {
      this.subjectList = res.rows
    })
    var teacherId = localStorage.getItem("teacherId")
    if (teacherId != null) {
      this.queryParams.teacherId = teacherId
    }
    this.getList();
  },
  methods: {
    handleCourseRecord(info) {
      this.reset();
      this.form.courseId = info.id
      if (info.attendanceRecord != null) {
        this.form = info.attendanceRecord
        this.form.courseId = info.id
      }
      this.title = "课程考勤";
      this.openCourseRecord = true;
    },
    handleCourseHomework(info) {
      this.reset();
      this.form.courseId = info.id
      if (info.homework != null) {
        this.form = info.homework
        this.form.courseId = info.id
      }
      this.title = "课程作业";
      this.openCourseHomework = true;
    },
    handleCourseExercises(info) {
      this.reset();
      this.form.courseId = info.id
      if (info.homeworkExercises != null) {
        this.form = info.homeworkExercises
        this.form.courseId = info.id
      }
      this.title = "课后练习";
      this.openCourseExercises = true;
    },
    handleCourseContent(info) {
      this.reset();
      this.form.courseId = info.id
      if (info.courseContent != null) {
        this.form = info.courseContent
        this.form.courseId = info.id
      }
      this.title = "课程内容";
      this.openCourseContent = true;
    },

    handleCourseVideo(info) {
      this.reset();
      this.form.courseId = info.id
      if (info.courseVideo != null) {
        this.form = info.courseVideo
        this.form.courseId = info.id
      }
      this.title = "教学视频";
      this.openCourseVideo = true;
    },
    handleAttendanceRecord(info) {
      var data = {
        courseId: info.id
      }
      addRecord(data).then(res => {
        if (res.code == 200) {
          this.$modal.msgSuccess("操作成功")
        } else {
          this.$modal.msgError("操作失败")
        }
        this.getList();
      })
    },
    /** 查询课程管理列表 */
    getList() {
      this.loading = true;
      listCourse(this.queryParams).then(response => {
        this.courseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openCourseContent = false
      this.openCourseVideo = false
      this.openCourseExercises = false 
      this.openCourseHomework = false 
      this.openCourseRecord = false 

      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        courseId: null,
        teacherId: null,
        name: null,
        parentId: null,
        introduction: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCourse(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程管理";
      });
    },
    submitRecordForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openCourseRecord = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openCourseRecord = false;
              this.getList();
            });
          }
        }
      });
    },
    submitHomeworkForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHomework(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openCourseHomework = false;
              this.getList();
            });
          } else {
            addHomework(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openCourseHomework = false;
              this.getList();
            });
          }
        }
      });
    },
    submitopenCourseExercisesForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateExercises(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openCourseExercises = false;
              this.getList();
            });
          } else {
            addExercises(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openCourseExercises = false;
              this.getList();
            });
          }
        }
      });
    },
    submitVideoForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateVideo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openCourseVideo = false;
              this.getList();
            });
          } else {
            addVideo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openCourseVideo = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitContentForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateContent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openCourseContent = false;
              this.getList();
            });
          } else {
            addContent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openCourseContent = false;
              this.getList();
            });
          }
        }
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCourse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除课程管理编号为"' + ids + '"的数据项？').then(function () {
        return delCourse(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/course/export', {
        ...this.queryParams
      }, `course_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>